<template>
  <div class="list" ref="singerWrapper">
    <ul class="list-content">
      <li v-for="group in data" :key="group.id" :ref="group.title">
        <div class="TitBox">
          <h3 class="SinTit">{{ group.title }}</h3>
        </div>
        <ul>
          <li v-for="item in group.items" :key="item.id" class="SingerBox" @click="selectItem(item)">
            <img v-lazy="item.avatar" class="avatar">
            <span>{{ item.name }}</span>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
import BScroll from 'better-scroll'

export default {
  props: {
    data: {
      type: Array
    },
    letter: {
      type: String,
      default: ''
    }
  },
  methods: {
    selectItem (item) {
      this.$emit('select', item)
      // console.log(item)
    }
  },
  mounted () {
    const options = {
      click: true,
      tap: true
    }
    var timerId = setInterval(() => {
      if (this.data.length !== 0) {
        console.log(8888888)
        // console.log(this.data)
        this.scroll = new BScroll(this.$refs.singerWrapper, options)
        clearInterval(timerId)
        console.log(this.scroll)
        // console.log(999)
      }
    }, 200)
    var divs = document.querySelector('.list')
    // var SingerHeight = document.body.clientHeight - 80
    var SingerHeight = window.screen.height - 78
    // console.log(SingerHeight)
    divs.style.height = SingerHeight + 'px'
  },
  watch: {
    letter (letter) {
      if (this.letter === '热') {
        // console.log(9999)
        const element = this.$refs['热门'][0]
        this.scroll.scrollToElement(element)
      } else {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
      // console.log(letter)
      // console.log(this.$refs[this.letter])
      // console.log(this.$refs)
    }
  }
}
</script>

<style scoped lang="less">
ul, li, h3{
  margin: 0;
  padding: 0;
  list-style: none;
}
.list {
  /*position: relative;
  left: 0;
  top: 1px;
  right: 0;
  bottom: 0;
  height: 100%;
  overflow: hidden;
  width: 100%;*/
  /*position: fixed;*/
  /*top: 1px;*/
  overflow: hidden;
  height: 100%;
}
.list-content {
  /*overflow: hidden;*/
  .TitBox {
    height: 40px;
    /*line-height: 40px;*/
    .SinTit {
      background-color: #ccc;
      color: #fff;
      margin-top: 10px;
      font-size: 17px;
    }
  }
  .SingerBox {
    position: relative;
    border-bottom: 1px solid #ccc;
    height: 64px;
    padding-top: 12px;
    /*line-height: 70px;*/
    span {
      display: inline-block;
      padding-left: 15px;
      position: absolute;
      top: 26px;
    }
  }
}
.avatar {
  height: 55px;
  width: 55px;
  border-radius: 5px;
}
</style>
